﻿using UnityEngine;
using System.Collections;

public enum ENUM_TGAMESTATE
{
    GAMESTATE_PREPARE = 0, // 初始化中 
    GAMESTATE_REGIST = 1, // 
	GAMESTATE_CREATEPLAYER = 2,     //】创建角色
    GAMESTATE_SELECTROLE = 3,	//选择角色
	GAMESTATE_PLAY = 4,        //游戏状态
	GAMESTATE_LOADING = 5,     //加载场景中
	
}

public class TGameState : MonoBehaviour {
	
	public GameObject cam_SelectRole;// 选择角色界面专用
	public GameObject cam_CreateRole;// 创建角色界面专用
	
	
	ENUM_TGAMESTATE gameState;
	
	static TGameState instance;
	
	public static TGameState GetInstance()
	{
		return instance;
	}
	
	void Awake()
	{
		instance = this;
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	public ENUM_TGAMESTATE CurrGameState
	{
		get{return gameState;}
		set{
			
			if(gameState != value)
			{
				gameState = value;
				
				ExecGameState(gameState);
			}
		}
	}
	
	public void ExecGameState(ENUM_TGAMESTATE iState)
    {		
		if(iState == ENUM_TGAMESTATE.GAMESTATE_SELECTROLE)
		{
//			UIManager.CleanOpendList();
		}
		
		cam_SelectRole.active = false;
		cam_CreateRole.active = false;
		
		if(iState != ENUM_TGAMESTATE.GAMESTATE_LOADING)
		{
			UIManager.HideAllWindow();
		}
		
		switch (iState)
        {
			case ENUM_TGAMESTATE.GAMESTATE_LOADING:
				break;
            case ENUM_TGAMESTATE.GAMESTATE_PREPARE:
				cam_CreateRole.active =true;
                break;
            case ENUM_TGAMESTATE.GAMESTATE_REGIST:
                break;
            case ENUM_TGAMESTATE.GAMESTATE_PLAY:
				cam_SelectRole.active = false;
				SetGameStatePlay();
                break;
            case ENUM_TGAMESTATE.GAMESTATE_SELECTROLE:
                break;
			case ENUM_TGAMESTATE.GAMESTATE_CREATEPLAYER:
                break;
         	default:
			   	Debug.LogError("wrong case");
                break;
        }
    }
	
	/**
	 * 设置进入游戏状态
	 */ 
    public void SetGameStatePlay()
    {
		if(ObjectManager.GetMainCamera()==null)
			return;
		if(ObjectManager.GetMainCamera().GetComponent<CamFollow>()==null)
		{
			ObjectManager.GetMainCamera().gameObject.AddComponent<CamFollow>();
		}
        ObjectManager.GetMainCamera().GetComponent<CamFollow>().enabled = true;
		
		isUIOpened = false;
		StartCoroutine(DelayPlay());
	}
	
	public bool isUIOpened = false;
	IEnumerator DelayPlay()
	{
//		while(UIResourcesStepDowanload.GetInst().playList.Count > 0 || !LoaderManager.GetInst().initSceneOver)
//		{
//			if(curGameState != ENUM_TGAMESTATE.GAMESTATE_LOADING)
//				curGameState = ENUM_TGAMESTATE.GAMESTATE_LOADING;
//			LoadingWindow window = LoadingWindow.GetInst();
//			if(window != null && !window.visible)
//				window.Open(true);
//				
//			yield return 0; 
//		}
		
		while(!LoaderManager.GetInstance().IsLoadOverWindow())
		{
			if(gameState != ENUM_TGAMESTATE.GAMESTATE_LOADING)
				gameState = ENUM_TGAMESTATE.GAMESTATE_LOADING;
			LoadingWindow window = LoadingWindow.GetInstance();
			if(window != null && !window.Visible)
				window.Open(true);
			
			yield return 0;
		}
		
		yield return new WaitForSeconds(0.5f);
		
		
		WWWObjectManager.GetInst().OpenAutoReleasUnusedResources();
		if(gameState != ENUM_TGAMESTATE.GAMESTATE_PLAY)
			gameState = ENUM_TGAMESTATE.GAMESTATE_PLAY;
		
		isUIOpened = true;
		
		UIResourcesStepDowanload.GetInst().StartPreLoad();
    }
	
	public bool IsPlaying()
	{
		return gameState == ENUM_TGAMESTATE.GAMESTATE_PLAY;
	}
	
	public bool IsLoading()
	{
		return gameState == ENUM_TGAMESTATE.GAMESTATE_LOADING;
	}
}
